package myJs.user.workflow.featureSelection

import myJs.cps._
import myJs.myPkg.reactHookForm._
import myJs.tool.Tool.messages
import myJs.Implicits._

import scala.scalajs.js

import japgolly.scalajs.react.vdom.all._
import japgolly.scalajs.react._
import japgolly.scalajs.react.React.Fragment
import japgolly.scalajs.react.util.EffectCatsEffect._
import myJs.myPkg.Hooks._
import cats.effect._
import myJs.Implicits._
import myJs.Utils._

/** Created by yz on 3/11/2020
  */
object FileSelectCp {

  case class Props(
      fileSelectShow:  SyncIO[Unit],
      registerOptions: RegisterOptions = RegisterOptions(),
      label: String,
      name: String,
      value: String = "",
      show: Boolean = false,
      questionInfo: String = ""
  ) {
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val formState = ReactHookForm.useFormState(options =
      UseFormStateOptions(
        name = props.name
      )
    )
    object FTool {}

    val hasErrorCls = if (formState.errors.isDefinedAt(props.name)) "has-error" else ""
    val displayStr = if (props.show) "block" else "none"

    div(
      display := displayStr,
      className := s"form-group col-sm-12 ${hasErrorCls}",
      label(
        className := s"",
        s"${props.label}",
        if (props.questionInfo.nonBlank) {
          span(
            className := "",
            a(
              `type` := "button",
              className := "selfQuestion",
              title := messages("note"),
              dataContainer := "body",
              dataToggle := "popover",
              dataHtml := "true",
              dataPlacement := "left",
              dataContent := props.questionInfo,
              dataTrigger := "hover",
              i(className := "fa fa-question-circle-o")
            )
          )
        } else EmptyVdom,
        ":"
      ),
      div(
        paddingLeft := 0.px, paddingRight := 0.px,
        className := "col-sm-8",
        PureInputCp.Props(
          name = props.name,
          readOnly = true,
          registerOptions = props.registerOptions,
          value = props.value
        ).render
      ),
      div(
       paddingLeft := 5.px, paddingRight := 0.px,
        className := "col-sm-4",
        button(
          width := "100%",
          `type` := "button",
          className := "btn green",
          onClick --> props.fileSelectShow,
          title := messages("selectFile"),
          i(className := "fa fa-file"),
          s" "
        )
      )
    )

  }

}
